package com.cn.demo.service.impl;

import com.cn.demo.entity.Design;
import com.cn.demo.entity.Experiment;
import com.cn.demo.mapper.DesignMapper;
import com.cn.demo.mapper.DesignTeamMapper;
import com.cn.demo.service.DesignService;
import com.cn.demo.service.ExperimentService;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class DesignServiceImpl implements DesignService {
    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Autowired
    private DesignTeamMapper designTeamMapper;

    @Override
    public Result getDesignSubject(String studentId, String curriculumDesignId) {
        return  Result.success(designMapper.getDesignSubject(studentId,curriculumDesignId));
    }

    @Override
    public Result getDesignSubjectAll(String studentId, String curriculumDesignId, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Design> designList = designMapper.getOrganizationStudentDesignTeam(studentId, curriculumDesignId);
        return Result.success(pageInfoUtilServer.currency(designList));
    }

    @Override
    public Result establishDesign(String studentId, String subject,String curriculumDesignId) {
        Design design = new Design();
        design.setDesignId(UUID.randomUUID().toString());
        design.setSubject(subject);
        design.setCurriculumDesignId(curriculumDesignId);
        if(designMapper.insert(design)>0){
            if (designTeamMapper.joinTeam(design.getDesignId(),studentId)>0) {
                return Result.success();
            }
        }
        return Result.error();
    }

    @Override
    public Result designJoinTeam(String studentId, String designId) {
        if (designTeamMapper.joinTeam(designId,studentId)>0) {
            return Result.success();
        }
        return Result.error();
    }


}